local ganlu = fk.CreateSkill {

  name = "joy__ganlu",

  tags = { Skill.Compulsory, },

}



ganlu:addEffect(fk.EventPhaseStart, {
  name = "joy__ganlu",
  events = {fk.EventPhaseStart},
  anim_type = "control",
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(ganlu.name) and player.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local _,dat = room:askForUseActiveSkill(player, "joy__ganlu_active", "#joy__ganlu-invoke", true)
    if dat then
      local targets = table.map(dat.targets, Util.Id2PlayerMapper)
      if dat.interaction == "joy__ganlu_move" then
        room:askForMoveCardInBoard(player, targets[1], targets[2], ganlu.name, "e", nil)
      else
        local slots = {}
        for _, id in ipairs(targets[1]:getCardIds("e")) do
          local s = Fk:getCardById(id).sub_type
          if targets[2]:getEquipment(s) ~= nil then
            table.insertIfNeed(slots, Util.convertSubtypeAndEquipSlot(s))
          end
        end
        if #slots == 0 then return end
        local choice = room:askForChoice(player, slots, ganlu.name)
        local id1 = targets[1]:getEquipment(Util.convertSubtypeAndEquipSlot(choice))
        local id2 = targets[2]:getEquipment(Util.convertSubtypeAndEquipSlot(choice))
        local moveInfos = {}
        table.insert(moveInfos, {
          from = targets[1].id,
          ids = {id1},
          toArea = Card.Processing,
          moveReason = fk.ReasonExchange,
          proposer = player.id,
          skillName = ganlu.name,
        })
        table.insert(moveInfos, {
          from = targets[2].id,
          ids = {id2},
          toArea = Card.Processing,
          moveReason = fk.ReasonExchange,
          proposer = player.id,
          skillName = ganlu.name,
        })
        room:moveCards(table.unpack(moveInfos))
        moveInfos = {}
        if not targets[2].dead and room:getCardArea(id1) == Card.Processing
        and targets[2]:hasEmptyEquipSlot(Fk:getCardById(id1).sub_type) then
          table.insert(moveInfos, {
            ids = {id1},
            to = targets[2].id,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonExchange,
            proposer = player.id,
            skillName = ganlu.name,
          })
        end
        if not targets[1].dead and room:getCardArea(id2) == Card.Processing
        and targets[1]:hasEmptyEquipSlot(Fk:getCardById(id2).sub_type) then
          table.insert(moveInfos, {
            ids = {id2},
            to = targets[1].id,
            toArea = Card.PlayerEquip,
            moveReason = fk.ReasonExchange,
            proposer = player.id,
            skillName = ganlu.name,
          })
        end
        if #moveInfos > 0 then
          room:moveCards(table.unpack(moveInfos))
        end
        local to_throw = {}
        if room:getCardArea(id1) == Card.Processing then table.insert(to_throw, id1) end
        if room:getCardArea(id2) == Card.Processing then table.insert(to_throw, id2) end
        if #to_throw > 0 then
          room:moveCards({
            ids = to_throw,
            toArea = Card.DiscardPile,
            moveReason = fk.ReasonPutIntoDiscardPile,
          })
        end
      end
    else
      player:drawCards(1, ganlu.name)
    end
  end,
})

return ganlu